package com.leapp.sk.viewdelegate.adapter.task

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.delegate.BaseAdapterDelegate
import com.example.mvp.view.GlideRoundTransform
import com.leapp.sk.R
import com.leapp.sk.bean.CompleteTaskEntity

/**
 * 视频作业是适配器视图托管类
 * @author  chaohaipeng  on 2018/8/3.
 */
class VideoTaskAdapterDelegate : BaseAdapterDelegate(){
    /**
     * 图片加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()
    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.adapter_task_video
    }

    /**
     * 设置数据
     */
    fun setData(context: Context, data: CompleteTaskEntity,isdetial:Boolean){
        val iv_atv_video_delete_btn=  getView<ImageView>(R.id.iv_atv_video_delete_btn)
        if (isdetial){
            iv_atv_video_delete_btn.visibility= View.GONE
        }else{
            iv_atv_video_delete_btn.visibility= View.VISIBLE
        }
        val rl_atv_video_rootView= getView<RelativeLayout>(R.id.rl_atv_video_rootView)
        val iv_atv_video_image= getView<ImageView>(R.id.iv_atv_video_image)
        val layoutParams: LinearLayout.LayoutParams = rl_atv_video_rootView?.layoutParams as LinearLayout.LayoutParams
        layoutParams.width= LKScreenUtil.getScreenWidth()/3- LKScreenUtil.dp2px(10f)
        layoutParams.height= LKScreenUtil.getScreenWidth()/3- LKScreenUtil.dp2px(10f)
        rl_atv_video_rootView?.layoutParams=layoutParams
        initvideo(context, iv_atv_video_image, data.url)
    }

    /**
     * 初始化图片
     */
    fun initvideo(context: Context, imageView: ImageView, imagePath: String){
        /**
         * 图片圆角处理
         */
        var mGlideRoundTransform: GlideRoundTransform = GlideRoundTransform.getInstance(context)
        mGlideRoundTransform.setExceptCorner(LKScreenUtil.dp2px(5f),true,true,true,true)
        val tag = imageView.getTag(R.id.tag_image)
        if ( tag==null||!tag?.equals(imagePath)) {
            val mRequestManager = Glide.with(context)
            mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
            mRequestOptions.skipMemoryCache(true)
            mRequestOptions.error(R.mipmap.icon_im_image_def)
            mRequestOptions.transforms(mGlideRoundTransform)
            mRequestManager?.load(imagePath)
                    ?.apply(mRequestOptions)
                    ?.into(imageView)
            imageView.setTag(R.id.tag_image,imagePath)
        }
    }


}